﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace OOXMLGenerator.Utils
{
    internal class ImageUtil
    {
        /// <summary>
        /// Converts Image to its bytes string representation.
        /// </summary>
        /// <param name="image">Image parameter that is converted</param>
        /// <returns>   - string representation of Image bytes or 
        ///             - empty string if Image parameter is null</returns>
        internal static string ImageToString(Image image)
        {
            try
            {
                if (image == null) return string.Empty;

                byte[] imageBytes = ImageToByte(image);
                return ByteToString(imageBytes);
            }
            catch (Exception ex) { throw ex; }
        }

        /// <summary>
        /// Converts Image to its array of bytes representation
        /// </summary>
        /// <param name="image">Image parameter that is converted</param>
        /// <returns>Image byte array representation</returns>
        internal static byte[] ImageToByte(Image image)
        {
            try
            {
                ImageConverter converter = new ImageConverter();
                byte[] imageBytes = (byte[])converter.ConvertTo(image, typeof(byte[]));
                return imageBytes;
            }
            catch (Exception ex) { throw ex; }
        }
        
        /// <summary>
        /// Converts an array of bytes to its string representation.
        /// </summary>
        /// <param name="bytes">Array of bytes</param>
        /// <returns>string representation of bytes array parameter </returns>
        internal static string ByteToString(byte[] bytes)
        {
            try
            {
                return Convert.ToBase64String(bytes);
            }
            catch (Exception ex) { throw ex; }
        }
    }
}
